using UnityEngine;

public class CharacterStep : MonoBehaviour
{
	[SerializeField]
	private Rigidbody leftLeg;

	[SerializeField]
	private Rigidbody rightLeg;

	[SerializeField]
	private Rigidbody leftElbow;

	[SerializeField]
	private Rigidbody leftHand;

	[SerializeField]
	private Rigidbody rightElbow;

	[SerializeField]
	private Rigidbody rightHand;

	[SerializeField]
	private CharacterJoint leftKnee;

	[SerializeField]
	private CharacterJoint rightKnee;

	[SerializeField]
	private float torque;

	[SerializeField]
	private float force;

	[SerializeField]
	private CharacterInformation info;

	[SerializeField]
	private Transform hip;

	public float legAngle = 70f;

	public float switchTime = 0.3f;

	private float counter;

	[SerializeField]
	private float side = 1f;

	private bool running;

	private void Update()
	{
		if (!info.isAlive)
		{
			running = false;
			return;
		}
		counter += Time.deltaTime;
		if (running && info.sinceGrounded < 0.1f)
		{
			leftLeg.AddTorque(hip.right * Time.deltaTime * side * torque, ForceMode.Acceleration);
			rightLeg.AddTorque(-hip.right * Time.deltaTime * side * torque, ForceMode.Acceleration);
			leftElbow.AddForce(hip.forward * Time.deltaTime * force, ForceMode.Acceleration);
			leftHand.AddForce(hip.forward * Time.deltaTime * force, ForceMode.Acceleration);
			rightElbow.AddForce(hip.forward * Time.deltaTime * force, ForceMode.Acceleration);
			rightHand.AddForce(hip.forward * Time.deltaTime * force, ForceMode.Acceleration);
		}
		if (Vector3.Angle(rightLeg.transform.up, leftLeg.transform.up) > legAngle && counter > switchTime && info.sinceGrounded < 0.1f && running)
		{
			side *= -1f;
			counter = 0f;
		}
	}

	public void PlayRunAnimation()
	{
		running = true;
		leftKnee.twistLimitSpring = new SoftJointLimitSpring
		{
			spring = 40f,
			damper = 5f
		};
		rightKnee.twistLimitSpring = new SoftJointLimitSpring
		{
			spring = 40f,
			damper = 5f
		};
	}

	public void StopRunAnimation()
	{
		running = false;
		leftKnee.twistLimitSpring = new SoftJointLimitSpring
		{
			spring = 4f,
			damper = 0f
		};
		rightKnee.twistLimitSpring = new SoftJointLimitSpring
		{
			spring = 4f,
			damper = 0f
		};
	}
}
